package cn.wxz1997.permission.service.impl;

import cn.wxz1997.permission.dao.DeptMapper;
import cn.wxz1997.permission.dao.RoleUserMapper;
import cn.wxz1997.permission.dao.UserMapper;
import cn.wxz1997.permission.dto.UserForm;
import cn.wxz1997.permission.model.Role;
import cn.wxz1997.permission.model.User;
import cn.wxz1997.permission.pojo.RoleUser;
import cn.wxz1997.permission.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Description: ${todo}
 * @Author: wxz1997
 * @Date 18-8-7下午10:12
 */
@Slf4j
@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private RoleUserMapper roleUserMapper;

    @Autowired
    private ModelMapper modelMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public User add(UserForm userForm) {
        log.info("userForm:" + userForm);
        User user = modelMapper.map(userForm, User.class);
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        user.setCreateTime(new Date());
        userMapper.insertSelective(user);
        List<Integer> roleIds = userForm.getRoleIds();
        if (null != roleIds){
            for (Integer roleId: roleIds){
                log.info("roleId:" + roleId);
                RoleUser roleUser = new RoleUser();
                roleUser.setUserId(user.getId());
                roleUser.setRoleId(roleId);
                roleUserMapper.insertSelective(roleUser);
            }
        }
        return userMapper.selectByPrimaryKey(user.getId());
    }

    @Override
    public User selectById(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public User selectByUsername(String username) {
        return userMapper.selectByUsername(username);
    }

    /**
     * 获取当前用户
     * @return
     */
    @Override
    public User getCurrentUser() {
        return (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    }

    @Override
    public List<User> list() {
        return userMapper.selectAll();
    }

    @Override
    public User update(UserForm userForm) {
        User user = modelMapper.map(userForm, User.class);
        User oldUser = userMapper.selectByPrimaryKey(user.getId());
        if (!oldUser.getPassword().equals(user.getPassword().trim())){
            user.setPassword(passwordEncoder.encode(user.getPassword()));
        }
        userMapper.updateByPrimaryKeySelective(user);
        roleUserMapper.deleteByUserId(user.getId());
        List<Integer> roleIds = userForm.getRoleIds();
        if (null != roleIds){
            for (Integer roleId: roleIds){
                RoleUser roleUser = new RoleUser();
                roleUser.setUserId(user.getId());
                roleUser.setRoleId(roleId);
                roleUserMapper.insertSelective(roleUser);
            }
        }
        return userMapper.selectByPrimaryKey(user.getId());
    }

    @Override
    public Integer delete(Integer id) {
        roleUserMapper.deleteByUserId(id);
        return userMapper.deleteByPrimaryKey(id);
    }
}
